package secondBrush.hash;

import java.util.HashMap;

/**
 * 四数相加
 */
public class E05Leetcode454 {
    public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int res = 0;
        for (int i : nums1) {
            for (int j : nums2) {
                int sum = i + j;
                map.put(sum, map.getOrDefault(sum,0) + 1);

            }
        }

        for (int i : nums3) {
            for (int j : nums4) {
                int sum = i + j;
                if (map.containsKey(-sum)) {
                    res += map.getOrDefault(-sum,0);
                }

            }
        }
        return res;

    }

    public static void main(String[] args) {
        int[] nums1 = {-1,-1};
        int[] nums2 = {-1,1};
        int[] nums3 = {-1,1};
        int[] nums4 = {1,-1};

        System.out.println(fourSumCount(nums1, nums2, nums3, nums4));

    }
}
